/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.mgrImpl.mgrMethods;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.dtgenerator.DotNetToJavaStringHelper;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class MgrImpDeleteGenerator extends ClassMethodGenerator {
  private static final String nodeCodesVarName = "nodeCodes";
  private static final String childIdsVarName = "hierachyIds";
  private GspBizEntityObject node;
  public MgrImpDeleteGenerator(GspBizEntityObject node) {
    this.node = node;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    buildNodeCodes(block);
    buildIds(block);
    block.statements().add(buildPerformDelete());
    return block;
  }

  private void buildIds(Block block) {
    VariableDeclarationFragment idsDeclaration = ast.newVariableDeclarationFragment();
    idsDeclaration.setName(ast.newSimpleName(childIdsVarName));
    ClassInstanceCreation creation = ast.newClassInstanceCreation();
    creation.setType(TypeInfo.getArrayListOfStringInfo().getType(ast));
    idsDeclaration.setInitializer(creation);
    VariableDeclarationStatement idsDeclarationStatement =
        ast.newVariableDeclarationStatement(idsDeclaration);
    idsDeclarationStatement.setType(TypeInfo.getArrayListOfStringInfo().getType(ast));
    block.statements().add(idsDeclarationStatement);

    GspBizEntityObject node = this.node;
    java.util.Stack<String> stack = new java.util.Stack<String>();

    while (node.getParentObject() != null) {
      node = node.getParentObject();
      stack.push(formatParameter(node.getCode()));
    }
    while (stack.size() > 0) {
      MethodInvocation addInvocation = ast.newMethodInvocation();
      addInvocation.setExpression(ast.newSimpleName(childIdsVarName));
      addInvocation.setName(ast.newSimpleName("add"));
      addInvocation.arguments().add(ast.newSimpleName(stack.pop() + "Id"));
      block.statements().add(ast.newExpressionStatement(addInvocation));
    }
  }

  private void buildNodeCodes(Block block) {
    VariableDeclarationFragment idsDeclaration = ast.newVariableDeclarationFragment();
    idsDeclaration.setName(ast.newSimpleName(nodeCodesVarName));
    ClassInstanceCreation creation = ast.newClassInstanceCreation();
    creation.setType(TypeInfo.getArrayListOfStringInfo().getType(ast));
    idsDeclaration.setInitializer(creation);
    VariableDeclarationStatement idsDeclarationStatement =
        ast.newVariableDeclarationStatement(idsDeclaration);
    idsDeclarationStatement.setType(TypeInfo.getArrayListOfStringInfo().getType(ast));
    block.statements().add(idsDeclarationStatement);

    GspBizEntityObject node = this.node;
    java.util.Stack<String> stack = new java.util.Stack<String>();
    while (node.getParentObject() != null) {
      stack.push(node.getCode());
      node = node.getParentObject();
    }

    while (stack.size() > 0) {
      MethodInvocation addInvocation = ast.newMethodInvocation();
      StringLiteral idLiteral = ast.newStringLiteral();
      idLiteral.setLiteralValue(stack.pop());
      addInvocation.setExpression(ast.newSimpleName(nodeCodesVarName));
      addInvocation.setName(ast.newSimpleName("add"));
      addInvocation.arguments().add(idLiteral);
      block.statements().add(ast.newExpressionStatement(addInvocation));
    }
  }

  private Statement buildPerformDelete() {

    MethodInvocation deleteChildInvocation = ast.newMethodInvocation();
    deleteChildInvocation.setName(ast.newSimpleName("deleteChild"));
    MethodInvocation getbeManagerInvocation = ast.newMethodInvocation();
    getbeManagerInvocation.setName(ast.newSimpleName("getBEManager"));
    MethodInvocation getMgrContextInvocation = ast.newMethodInvocation();
    getMgrContextInvocation.setName(ast.newSimpleName("getBEManagerContext"));
    getbeManagerInvocation.setExpression(getMgrContextInvocation);
    deleteChildInvocation.setExpression(getbeManagerInvocation);
    deleteChildInvocation.arguments().add(ast.newSimpleName(nodeCodesVarName));
    deleteChildInvocation.arguments().add(ast.newSimpleName(childIdsVarName));
    deleteChildInvocation.arguments().add(ast.newSimpleName("ids"));
    return ast.newExpressionStatement(deleteChildInvocation);
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>(
        java.util.Arrays.asList(ModifierKeyword.PUBLIC_KEYWORD));
  }

  @Override
  protected String getMethodName() {
    if (node.getCode().equals("Child")) {
      return "delete" + "ChildNode";
    }
    return "delete" + node.getCode();
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    GspBizEntityObject node = this.node;
    java.util.Stack<String> stack = new java.util.Stack<String>();
    int paramCount = 0;

    while (node.getParentObject() != null) {
      paramCount++;
      node = node.getParentObject();
      stack.push(formatParameter(node.getCode()));
    }
    java.util.ArrayList<ParameterInfo> list = new java.util.ArrayList<ParameterInfo>();
    for (int i = 0; i < paramCount; i++) {
      ParameterInfo tempVar = new ParameterInfo();
      tempVar.setParamName(stack.pop() + "Id");
      tempVar.setParamType(com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(String.class));
      list.add(tempVar);
    }
    ParameterInfo tempVar2 = new ParameterInfo();
    tempVar2.setParamName("ids");
    TypeInfo typeInfo = new TypeInfo(java.util.List.class);
    typeInfo.setIsParameterizedType(true);
    ArrayList<TypeInfo> genericTypes = new ArrayList<>();
    genericTypes.add(new TypeInfo(String.class));
    typeInfo.setArguments(genericTypes);
    tempVar2.setParamType(typeInfo);
    list.add(tempVar2);

    return list;
  }

  private String formatParameter(String source) {
    if (!DotNetToJavaStringHelper.isNullOrEmpty(source)) {
      if (source.length() > 1) {
        return Character.toLowerCase(source.charAt(0)) + source.substring(1);
      }
      return String.valueOf(Character.toLowerCase(source.charAt(0)));
    }
    return null;
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo("void");
  }
}
